package com.wef.system.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlReturnResultSet;

import com.gfa4j.dao.impl.CommonDAOJDBCImpl;
import com.gfa4j.service.impl.BaseServiceImpl;
import com.wef.system.entity.Role;
import com.wef.system.entity.User;
import com.wef.system.service.TestJdbcTemplateService;

public class TestJdbcTemplateServiceImpl extends BaseServiceImpl implements TestJdbcTemplateService{
	private CommonDAOJDBCImpl commonDAOJDBCImpl;

	public CommonDAOJDBCImpl getCommonDAOJDBCImpl() {
		return commonDAOJDBCImpl;
	}

	public void setCommonDAOJDBCImpl(CommonDAOJDBCImpl commonDAOJDBCImpl) {
		this.commonDAOJDBCImpl = commonDAOJDBCImpl;
	}

	public Map executePro(String inpare) {
		Map sqlParameters = new LinkedHashMap();
		
				sqlParameters.put(new SqlParameter("inpare", Types.VARCHAR), inpare);
				sqlParameters.put(new SqlReturnResultSet("resultSet", new RowMapper() {
					public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
						User user = new User();
						user.setUserId(arg0.getString("user_id"));
						user.setUserCode(arg0.getString("user_code"));
						user.setUserName(arg0.getString("user_name"));
						return user;
					}
				}), null);
				sqlParameters.put(new SqlOutParameter("outpare1", Types.INTEGER), null);
		
				sqlParameters.put(new SqlReturnResultSet("resultSet2", new RowMapper() {
					public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
						Role role = new Role();
						role.setRoleId(arg0.getString("sys_role_id"));
						role.setRoleName(arg0.getString("role_name"));
						return role;
					}
				}), null);
		
				Map results = getCommonDAOJDBCImpl().execProc("test", sqlParameters);
		
				// --测试输出控制台
				System.out.println(results.get("outpare1").toString());
				List list = (List) results.get("resultSet");
				Iterator it = list.iterator();
				while (it.hasNext()) {
					User user = (User) it.next();
					System.out.print("name1 = " + user.getUserId() + "   ");
					System.out.print("name1 = " + user.getUserCode() + "   ");
					System.out.print("name1 = " + user.getUserName() + "   ");
					System.out.println();
				}
				List list2 = (List) results.get("resultSet2");
				Iterator it2 = list2.iterator();
				while (it2.hasNext()) {
					Role role = (Role) it2.next();
					System.out.print("name1 = " + role.getRoleId() + "   ");
					System.out.print("name2 = " + role.getRoleName() + "   ");
					System.out.println();
				}
				// --测试输出控制台
		
				return results;
	}

	
//	public Map executePro(String inpare) {
//		

//	}

	// JdbcTemplate jdbcTemplate;

	// public JdbcTemplate getJdbcTemplate() {
	// return jdbcTemplate;
	// }
	//
	// public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
	// this.jdbcTemplate = jdbcTemplate;
	// }

	// public Map executePro(String custNum) {
	// SearchCustomersProcedure sp = new SearchCustomersProcedure(this.jdbcTemplate.getDataSource());
	// Map inParams = new HashMap();
	// inParams.put("inpare", custNum);
	// inParams.put("resultSet", null);
	// inParams.put("outpare1", null);
	// inParams.put("resultSet2", null);
	//		
	// Map results = sp.execute(inParams);
	// System.out.println(results.get("outpare1").toString());
	//		
	// List list = (List)results.get("resultSet");
	// Iterator it = list.iterator();
	// while(it.hasNext()){
	// Customer customer = (Customer)it.next();
	// System.out.print("name1 = "+customer.getName1()+"   ");
	// System.out.print("name2 = "+customer.getName2());
	// System.out.println();
	// }
	// List list2 = (List)results.get("resultSet2");
	// Iterator it2 = list.iterator();
	// while(it2.hasNext()){
	// Customer customer = (Customer)it2.next();
	// System.out.print("name1 = "+customer.getName1()+"   ");
	// System.out.print("name2 = "+customer.getName2());
	// System.out.println();
	// }
	// return results;
	// }

	// public class SearchCustomersProcedure extends StoredProcedure {
	// SearchCustomersProcedure(DataSource dataSource) {
	// super(dataSource, "test");
	// declareParameter(new SqlParameter("inpare", Types.VARCHAR));
	// declareParameter(new SqlReturnResultSet("resultSet", new DemoRowMapper()));
	// declareParameter(new SqlOutParameter("outpare1", Types.INTEGER));
	// declareParameter(new SqlReturnResultSet("resultSet2", new DemoRowMapper2()));
	// compile();
	// }
	// }
	//
	// public class DemoRowMapper implements RowMapper {
	// public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
	// Customer customer = new Customer();
	// customer.setName1(rs.getString(1));
	// customer.setName2(rs.getString(5));
	// return customer;
	// }
	// }
	//
	// public class DemoRowMapper2 implements RowMapper {
	// public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
	// Customer customer = new Customer();
	// customer.setName1(rs.getString(1));
	// customer.setName2(rs.getString(5));
	// return customer;
	// }
	// }
	//
	// public class Customer {
	// private String name1;
	// private String name2;
	//
	// public String getName1() {
	// return name1;
	// }
	//
	// public void setName1(String name1) {
	// this.name1 = name1;
	// }
	//
	// public String getName2() {
	// return name2;
	// }
	//
	// public void setName2(String name2) {
	// this.name2 = name2;
	// }
	// }

}